### Project 31: Rotary Potentiometer ![](media/f71165ab140ae6b2aac093dc75785c96.jpeg) **1. Introduction** In the previous courses, we did experiments of breathing light and controlling LED with button. In this course, we do these two experiments by controlling the brightness of LED through an adjustable potentiometer. The brightness of LED is controlled by PWM values, and the range of analog values is 0 to 4095 and the PWM value range is 0-255. After the code is set successfully, we can control the brightness of the LED on the module by rotating the potentiometer. **2. Required Components** ![image-20231020085536967](media/image-20231020085536967.png) **3. Connection Diagram** ![](media/7f24723673e622d23fbe0a3cdbd21d69.png) **4. Test Code** ```Python from machine import Pin,PWM,ADC import time pwm =PWM(Pin(15,Pin.OUT),1000) adc=ADC(Pin(34)) adc.atten(ADC.ATTN_11DB) adc.width(ADC.WIDTH_10BIT) try: while True: adcValue=adc.read() pwm.duty(adcValue) print(adc.read()) time.sleep_ms(100) except: pwm.deinit() ``` **5. Code Explanation** It is easy to control the brightness of the LED light by a potentiometer. Here we can find that MicroPython unifies the value range of the ADC between 0 and 1023, and assigns values directly, which is simple and convenient. **6. Test Result** Connect the wires according to the experimental wiring diagram and power on. Click ![](media/da852227207616ccd9aff28f19e02690.png)“Run current script”, the code starts executing. Rotating the potentiometer on the module can adjust the brightness of the LED on the LED module. Press “Ctrl+C”or click![](media/27451c8a9c13e29d02bc0f5831cfaf1f.png)“Stop/Restart backend”to exit the program.